package fun.zhufn.oldcomm.view;

import com.formdev.flatlaf.FlatLightLaf;
import fun.zhufn.oldcomm.UserManagerController;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.util.Locale;
import java.util.logging.Logger;

public class Login {
    private static final Logger log = Logger.getLogger(Login.class.toString());
    private static final UserManagerController userContro = UserManagerController.getInstance();
    public Login() {
        FlatLightLaf.setup();
        $$$setupUI$$$();
        UserManagerController.getInstance().getSavedUserListOrDefault();
        jbLogin.addActionListener(e -> {
            log.info("用户点击登录。。。");
            String username = textField1.getText();
            String pwd = String.valueOf(textField2.getPassword());
            userContro.doLogin(frame, username, pwd);
        });
    }

    /**
     * 启动界面
     */
    public void start() {
        log.info("启动登录界面！");
        try {
//            RootDesk.start();
//            if (true) return;
            frame = new JFrame("Login");
            frame.setContentPane(new Login().jpLogin);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setSize(300, 150);
            //frame.pack();
            frame.setVisible(true);
            //userContro.doLogin(frame, "root", "123");
        } catch (Exception e) {
            log.warning(e.toString());
        }
    }

    private static JFrame frame;
    private JPanel jpLogin;
    private JTextField textField1;
    private JPasswordField textField2;
    private JButton jbLogin;
    private JLabel jlUser;
    private JLabel jlPwd;

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jpLogin = new JPanel();
        jpLogin.setLayout(new GridBagLayout());
        jpLogin.setBackground(new Color(-402697));
        jpLogin.setForeground(new Color(-7472787));
        jpLogin.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(-1908252)), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, this.$$$getFont$$$(null, -1, -1, jpLogin.getFont()), null));
        textField1.setBackground(new Color(-1));
        Font textField1Font = this.$$$getFont$$$(null, -1, 16, textField1.getFont());
        if (textField1Font != null) textField1.setFont(textField1Font);
        textField1.setForeground(new Color(-16777216));
        textField1.setText("");
        textField1.setToolTipText("用户名");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 0, 0, 50);
        jpLogin.add(textField1, gbc);
        Font textField2Font = this.$$$getFont$$$(null, -1, 16, textField2.getFont());
        if (textField2Font != null) textField2.setFont(textField2Font);
        textField2.setForeground(new Color(-16777216));
        textField2.setText("");
        textField2.setToolTipText("密码");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(2, 0, 2, 50);
        jpLogin.add(textField2, gbc);
        jbLogin.setBackground(new Color(-4458500));
        jbLogin.setBorderPainted(true);
        Font jbLoginFont = this.$$$getFont$$$(null, -1, 16, jbLogin.getFont());
        if (jbLoginFont != null) jbLogin.setFont(jbLoginFont);
        jbLogin.setForeground(new Color(-16777216));
        jbLogin.setText("登录");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 3;
        gbc.weightx = 5.5;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 50, 0, 50);
        jpLogin.add(jbLogin, gbc);
        jlUser = new JLabel();
        jlUser.setText("用户名");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 0.5;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 50, 0, 0);
        jpLogin.add(jlUser, gbc);
        jlPwd = new JLabel();
        jlPwd.setText("密码");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.5;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 50, 0, 0);
        jpLogin.add(jlPwd, gbc);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpLogin;
    }

    private void createUIComponents() {
        textField1 = new JTextField();
        MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(192, 192,
                192));
        textField1.setBorder(border);
        textField2 = new JPasswordField();
        textField2.setBorder(border);

        border = new MatteBorder(2, 2, 2, 2, new Color(0xd1c2d3));
        jbLogin = new JButton();
        jbLogin.setBorder(border);
    }
}
